package com.lss.myapp.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.lss.myapp.R;
import com.lss.myapp.adapter.HomeAdapter;
import com.lss.myapp.databinding.FragmentHomeBinding;

import java.util.ArrayList;

public class HomeFragment extends Fragment {
    private String[] mTitles = new String[]{"关注", "推荐", "新闻", "游戏", "娱乐", "综艺"};
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private FragmentHomeBinding binding;

    private ViewPager mViewPager;
    private SlidingTabLayout mSlidingTabLayout;

    public static HomeFragment newInstance() {
        return new HomeFragment();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        mViewPager = view.findViewById(R.id.viewpager);
        mSlidingTabLayout = view.findViewById(R.id.slidingTabLayout);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        for (String mTitle : mTitles) {
            mFragments.add(VideoFragment.newInstance(mTitle));
        }
        mViewPager.setOffscreenPageLimit(mTitles.length);
        mViewPager.setAdapter(new HomeAdapter(getChildFragmentManager(), mTitles, mFragments));
        mSlidingTabLayout.setViewPager(mViewPager);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}